/*
* Service Unit List logical code
* tibco/admin/deployment/sa/su/sulist
* Created  by Amour GUO 04/29/2007
*/

if(!jsx3.lang.Package.forName("tibco.admin.deployment.sa.su.sulist")) {
    /*
    * Define sulist Package if not exist.
    */
    jsx3.lang.Package.definePackage("tibco.admin.deployment.sa.su.sulist", function (shdResDefs) {
    });
}

/**
 * define SuList(Service Unit List) class;
 * className: tibco.admin.deployment.sa.su.sulist.SuList;
 */
jsx3.lang.Class.defineClass(
        "tibco.admin.deployment.sa.su.sulist.SuList",
        jsx3.lang.Object,
        null,
        function(serviceUnit) {

            serviceUnit.block = null;
            //public root block;

            serviceUnit.saEntityID = null;
            //Service Assembly id
            serviceUnit.envEntityID = null;
            //Environment ID
            serviceUnit.suEntityID = null;
            //Service Unit Entity ID
            serviceUnit.selection = new jsx3.util.List(0);

            /**
             * Publish the SERVICEUNIT_ROW_SELECTED event after click a row of server unit list
             * @param objEvent {jsx3.gui.Matrix} affected matrix object(server unit list)
             */
            serviceUnit.selectSU = function(objEvent) {
                try {
                    var objRec = null;
                    ;
                    if(serviceUnit.ignoreSelects) {
                        var cacheDoc = objEvent.getXML();
                        if(cacheDoc) {
                            var recordIter = cacheDoc.getXML().selectNodes("//record[@jsxselected='1']");
                            while(recordIter.hasNext()) {
                                var record = recordIter.next();
                                record.setAttribute("jsxselected", 0);
                            }
                            var iter = serviceUnit.selection.iterator();
                            while(iter.hasNext()) {
                                var entityID = iter.next();
                                var record = cacheDoc.getXML().selectSingleNode("//record[@entityID='" + entityID + "']");
                                if(record) {
                                    record.setAttribute("jsxselected", 1);
                                }
                            }
                        }
                        objEvent.repaint();
                        return;
                    }
                    else {
                        serviceUnit.selection.clear();
                        var nodes = objEvent.getSelectedNodes();
                        var selectionSize = nodes.size();
                        for(var i = 0; i < selectionSize; i++) {
                            serviceUnit.selection.add(nodes.getItem(i).getAttribute('entityID'));
                        }
                        if(selectionSize == 1) {
                            objRec = nodes.getItem(0);
                        }
                    }

                    var message = new Object();
                    message.subject = tibco.admin.deployment.sa.su.sulist.EventID.SERVICEUNIT_ROW_SELECTED;
                    if(objRec) {
                        //Publish the SERVICEUNIT_ROW_SELECTED event.
                        serviceUnit.suEntityID = objRec.getAttribute("entityID");
                        message.saEntityID = parseInt(serviceUnit.saEntityID);
                        message.envEntityID = parseInt(serviceUnit.envEntityID);
                        message.name = objRec.getAttribute("name");
                        message.type = objRec.getAttribute("type");
                        message.suEntityID = parseInt(serviceUnit.suEntityID);
                    }
                    else {
                        serviceUnit.suEntityID = null;
                        message.suEntityID = null;
                    }
                    tibco.admin.PubSub.publish(message);
                }
                catch(ex) {
                    tibco.admin.Main.logException(ex);
                }
            }

            /* ------------- call getAllServiceUnitsSummary operation -------------*/
            serviceUnit.callgetAllServiceUnitsSummary = function() {
                var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/deployment/sa/su/sulist/rules/getAllServiceUnitsSummaryRule.xml", "getAllServiceUnitsSummary");
                objService.saEntityID = serviceUnit.saEntityID;
                objService.setEndpointURL(tibco.admin.util.ServiceHelper.getDeploymentEndpointURL());
                //subscribe and call
                objService.subscribe(jsx3.net.Service.ON_SUCCESS, serviceUnit.ongetAllServiceUnitsSummarySuccess);
                objService.doCall();
            };

            serviceUnit.ongetAllServiceUnitsSummarySuccess = function(objEvent) {
                var mx = serviceUnit.block.getDescendantOfName("mtxServiceUnit44");

                //if there is no record with hasError or isMapped attribute equals 'true', hide the relative image column for more place.
                var myXML = mx.getXML();
                var blankImage = tibco.admin.Main.getServer().getDynamicProperty("@addin@Blank Icon Path");
                var hasError = myXML.selectNodes("//record[@hasError!='" + blankImage + "']");
                var isMapped = myXML.selectNodes("//record[@isMapped!='" + blankImage + "']");
                if(hasError.getLength() < 1) {
                    mx.getChild("mcHasError44").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                }
                else {
                    mx.getChild("mcHasError44").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                }
                if(isMapped.getLength() < 1) {
                    mx.getChild("mcIsMapped44").setDisplay(jsx3.gui.Block.DISPLAYNONE, true);
                }
                else {
                    mx.getChild("mcIsMapped44").setDisplay(jsx3.gui.Block.DISPLAYBLOCK, true);
                }

                var iter = serviceUnit.selection.iterator();
                var doc = mx.getXML();
                while(iter.hasNext()) {
                    var entityID = iter.next();
                    var node = doc.selectSingleNode("//record[@entityID='" + entityID + "']");
                    if(node) {
                        mx.selectRecord(node.getAttribute("jsxid"));
                    }
                }
                serviceUnit.selectSU(mx);
                mx.repaint(true);

            };

            //SubPub(SERVICEASSEMBLY_ROW_SELECTED) Callback func.
            serviceUnit.setServiceUnits = function(message) {
                if(!message.entityID) return false;
                if(serviceUnit.saEntityID != message.entityID) serviceUnit.suEntityID = null;//if sa is changed, set suEntityID = null;
                serviceUnit.saEntityID = message.entityID;
                if(message.envEntityID) {
                    serviceUnit.envEntityID = message.envEntityID;
                }
                tibco.admin.deployment.sa.su.sulist.SuList.callgetAllServiceUnitsSummary();
            }

            serviceUnit.subscribeAll = function() {
                //Subscribe the SERVICEASSEMBLY_ROW_SELECTED event.
                tibco.admin.PubSub.subscribe(tibco.admin.deployment.saList.EventID.SERVICEASSEMBLY_ROW_SELECTED, serviceUnit.setServiceUnits);
            };

            serviceUnit.unsubscribeAll = function() {
                tibco.admin.PubSub.unsubscribe(tibco.admin.deployment.saList.EventID.SERVICEASSEMBLY_ROW_SELECTED, serviceUnit.setServiceUnits);
            }


            /**
             * MUST appear in "onAfterDeserialize" textarea.
             * @param objJSX {jsx3.gui.Object} the root JSX Object of current Component.
             */
            serviceUnit.onAfterDeserialize = function(objJSX) {
                serviceUnit.block = objJSX;
                objJSX.handler = serviceUnit;
                serviceUnit.subscribeAll();
            }

            serviceUnit.onDestroy = function(objTarget, objParent) {
                serviceUnit.selection.clear();
                serviceUnit.unsubscribeAll();
                serviceUnit.block = null;
            }

            serviceUnit.beginEdit = function() {
                serviceUnit.ignoreSelects = true;
            }

            serviceUnit.endEdit = function() {
                serviceUnit.ignoreSelects = false;
            }
        });